package com.inossem.warehousestorage_inossem.utils;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.inossem.warehousestorage_inossem.adapter.OverviewAdapter;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageManageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageMaterielListBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageMaterielListRequestBean;
import com.inossem.warehousestorage_inossem.bean.common.GetOperatorBean;
import com.inossem.warehousestorage_inossem.bean.common.GetOperatorGroupBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/8/16.
 */

public class ChangeStorageUtils {

    private static final String IS_FOCUS_TRUE = "1";//允许修改过账日期

    private static final String IS_FOCUS_FALSE = "0"; //不允许修改过账日期

    //获取转储信息
    public static ChangeStorageBean getChangeStorageBean(Context context) {
        String json = SPUtils.getChangeStorageInformation(context);
        if (TextUtils.isEmpty(json)) {
            return new ChangeStorageBean();
        } else {
            Gson gson = new Gson();
            return gson.fromJson(json, ChangeStorageBean.class);
        }
    }

    public static void saveChangeStorageBean(Context context, ChangeStorageBean changeStorageBean) {
        Gson gson = new Gson();
        SPUtils.saveChangeStorageInformation(context, gson.toJson(changeStorageBean, ChangeStorageBean.class));
    }

    public static ChangeStorageMaterielListRequestBean getChangeStorageMaterielListRequestBean (String condition, String inventoryAddressCode, String businessType, String specialInventory) {
        ChangeStorageMaterielListRequestBean bean = new ChangeStorageMaterielListRequestBean();
        bean.setCondition(condition);
        bean.setLocation_id(inventoryAddressCode);
        bean.setReceipt_type(businessType);
        bean.setSpec_stock(specialInventory);
        return bean;
    }
    public static void updateSpecialInventoryById(Context context,String mStockPositionId,String facId , String locationId, String matId,String number, String materielCode, String materielDescription, String factoryCode, String factoryValue, String inventoryAddressCode, String inventoryAddressValue, String warehouseNumber) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        List<ChangeStorageMaterielListBean.MaterielListBean> materielList = bean.getMaterielList();
        ChangeStorageMaterielListBean.MaterielListBean findMaterielItem = null;
        for (int i = 0; i < materielList.size(); i++) {
            ChangeStorageMaterielListBean.MaterielListBean materielItem = materielList.get(i);
            if (materielItem.getStock_position_id().equals(mStockPositionId) ) {
                findMaterielItem = materielItem;
                findMaterielItem.setNumber(number);
                if (!TextUtils.isEmpty(materielCode) && !TextUtils.isEmpty(materielDescription)) {
                    findMaterielItem.setTargetMaterielCode(materielCode);
                    findMaterielItem.setTargetMaterielDescription(materielDescription);
                }
                if (!TextUtils.isEmpty(factoryCode) && !TextUtils.isEmpty(inventoryAddressCode)) {
                    findMaterielItem.setTargetFactoryCode(factoryCode);
                    findMaterielItem.setTargetFactoryValue(factoryValue);
                    findMaterielItem.setTargetInventoryAddressCode(inventoryAddressCode);
                    findMaterielItem.setTargetInventoryAddressValue(inventoryAddressValue);
                    findMaterielItem.setTargetWarehouseNumber(warehouseNumber);
                    findMaterielItem.setTargetInventoryAddressId(locationId);
                    findMaterielItem.setTargetFactoryId(facId);
                }
                findMaterielItem.setTargetMaterielId(matId);
//                if (!TextUtils.isEmpty(specialInventoryCode) && !TextUtils.isEmpty(specialInventoryDescription)) {
//                    findMaterielItem.setTargetSpecialInventoryCode(specialInventoryCode);
//                    findMaterielItem.setTargetSpecialInventoryDescription(specialInventoryDescription);
//                }

                materielList.remove(materielItem);
                break;
            }
        }
        if (findMaterielItem != null) {
            materielList.add(0, findMaterielItem);
        }
        bean.setMaterielList(materielList);
        saveChangeStorageBean(context, bean);
    }

    public static ChangeStorageMaterielListBean.MaterielListBean getMaterielById(Context context,String mStockPositionId ) {
//        LogUtils.i("mMatId == " + mMatId + "     mLocationId === " + mLocationId + "    mStockPositionId === " + mStockPositionId + "   mBatch === " + mBatch);
        List<ChangeStorageMaterielListBean.MaterielListBean> materielList = getChangeStorageBean(context).getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            ChangeStorageMaterielListBean.MaterielListBean materielItem = materielList.get(i);
            if (materielItem.getStock_position_id().equals(mStockPositionId)) {

                return materielItem;
            }
        }
        return null;
    }

    public static List<ChangeStorageManageBean.DataBean.FtyListBean.LocationAryBean> getInventoryAddressListByFactoryCode(Context context, String factoryCode) {
        List<ChangeStorageManageBean.DataBean.FtyListBean> factoryList = getChangeStorageBean(context).getTargetFactoryList();
        for (int i = 0; i < factoryList.size(); i++) {
            ChangeStorageManageBean.DataBean.FtyListBean factoryItem = factoryList.get(i);
            if (factoryCode.equals(factoryItem.getFty_code())) {
                return factoryItem.getLocation_ary();
            }
        }
        return new ArrayList<>();
    }

    public static boolean hasMaterielList(Context context) {
        List<ChangeStorageMaterielListBean.MaterielListBean> materielList = getChangeStorageBean(context).getMaterielList();
        if (materielList == null || materielList.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public static void deleteMaterielList(Context context) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        bean.setMaterielList(new ArrayList<>());
        saveChangeStorageBean(context, bean);
    }

    public static void deleteMaterielById(Context context, String mMatId ,String mLocationId ,String mStockPositionId ,String mBatch) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        List<ChangeStorageMaterielListBean.MaterielListBean> materielList = bean.getMaterielList();
        if (materielList != null && !materielList.isEmpty()) {
            for (int i = 0; i < materielList.size(); i++) {
                ChangeStorageMaterielListBean.MaterielListBean materielItem = materielList.get(i);
                if ( materielItem.getStock_position_id().equals(mStockPositionId)) {
                    materielList.remove(i);
                    break;
                }
            }
        }
        bean.setMaterielList(materielList);
        saveChangeStorageBean(context, bean);
    }

    //检查转储管理物料列表是否至少填写一项
    public static int getChangeStorageMaterielNumber(Context context) {
        int result = 0;
        List<ChangeStorageMaterielListBean.MaterielListBean> materielList = getChangeStorageBean(context).getMaterielList();
        if (materielList == null || materielList.isEmpty()) {
            return result;
        }
        for (int i = 0; i < materielList.size(); i++) {
            ChangeStorageMaterielListBean.MaterielListBean materielItem = materielList.get(i);
            if (!TextUtils.isEmpty(materielItem.getNumber()) && Float.parseFloat(materielItem.getNumber()) > 0) {
                result = result + 1;
            }
        }
        return result;
    }

    //删除转储管理验收人
    public static void deleteOperator(Context context, String id) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        List<GetOperatorBean.OperatorBean> operatorList = bean.getUser_list();
        for (int i = 0; i < operatorList.size(); i++) {
            GetOperatorBean.OperatorBean operatorItem = operatorList.get(i);
            if (operatorItem.getUser_id().equals(id)) {
                operatorList.remove(operatorItem);
                break;
            }
        }
        bean.setUser_list(operatorList);
        saveChangeStorageBean(context, bean);
    }

    //是否有转储管理操作人员
    public static boolean hasOperator(Context context) {
        List<GetOperatorBean.OperatorBean> operatorList = getChangeStorageBean(context).getUser_list();
        if (operatorList != null && !operatorList.isEmpty()) {
            return true;
        }
        return false;
    }

    //添加转储管理验收人
    public static void addOperator(Context context, GetOperatorBean.OperatorBean receiveItem) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        List<GetOperatorBean.OperatorBean> receiverList = bean.getUser_list() == null ? new ArrayList<>() : bean.getUser_list();
        receiverList.add(0, receiveItem);
        bean.setUser_list(receiverList);
        saveChangeStorageBean(context, bean);
    }

    //拼领料出库对象
    public static ChangeStorageConfirmRequestBean getChangeStorageConfirmRequestBean(Context context, String voucherDate, String postDate, boolean isFocus) {
        ChangeStorageConfirmRequestBean result = new ChangeStorageConfirmRequestBean();
        ChangeStorageBean bean = getChangeStorageBean(context);
        result.setFty_id(bean.getFty_id());
        result.setLocation_id(bean.getLoc_id());
//        result.setWarehouseNumber(bean.getWarehouseNumber());
        result.setMove_type_id(bean.getMove_type_id());
        result.setReceipt_type(bean.getBusinessType());
        if (!TextUtils.isEmpty(bean.getStock_transport_id())) {
            result.setStock_transport_id(bean.getStock_transport_id());
        }
//        result.setVoucherDate(voucherDate);
//        result.setPostDate(postDate);
//        result.setBusinessType(bean.getBusinessType());
//        result.setDumpNumber(bean.getDumpNumber());
//        result.setIsFocus(isFocus ? IS_FOCUS_TRUE : IS_FOCUS_FALSE);
        //操作人
        List<ChangeStorageConfirmRequestBean.UserListBean> resultOperatorList = new ArrayList<>();
        List<GetOperatorBean.OperatorBean> operatorList = bean.getUser_list();
        for (int i = 0; i < operatorList.size(); i++) {
            ChangeStorageConfirmRequestBean.UserListBean operator = new ChangeStorageConfirmRequestBean.UserListBean();
            operator.setRole_id(operatorList.get(i).getRole_id());
            operator.setUser_id(operatorList.get(i).getUser_id());
            resultOperatorList.add(operator);
        }
        result.setUser_list(resultOperatorList);
        //物料
        List<ChangeStorageConfirmRequestBean.ItemListBean> resultMaterielList = new ArrayList<>();
        List<ChangeStorageMaterielListBean.MaterielListBean> materielList = bean.getMaterielList();
        String factoryId = bean.getFty_id();
        String locationId = bean.getLoc_id();
        for (int i = 0; i < materielList.size(); i++) {
            ChangeStorageConfirmRequestBean.ItemListBean resultMateriel = new ChangeStorageConfirmRequestBean.ItemListBean();
            ChangeStorageMaterielListBean.MaterielListBean materiel = materielList.get(i);
            resultMateriel.setStock_transport_rid(i + 1 + "");
            resultMateriel.setStock_position_id(materiel.getStock_position_id());
            resultMateriel.setMat_id(materiel.getMat_id());
            resultMateriel.setFty_id(factoryId);
            resultMateriel.setLocation_id(locationId);
//            resultMateriel.setLocation_id(materiel.getLocation_id());
            resultMateriel.setMat_input(materiel.getTargetMaterielId());
            resultMateriel.setFty_input(materiel.getTargetFactoryId());
            resultMateriel.setLocation_input(materiel.getTargetInventoryAddressId());
            resultMateriel.setTransport_qty(materiel.getNumber());
//            resultMateriel.setTransport_qty("10000");
            resultMateriel.setUnit_id(materiel.getUnit_id());
            if (!TextUtils.isEmpty(resultMateriel.getTransport_qty())) {
                resultMaterielList.add(resultMateriel);
            }
        }
        result.setItem_list(resultMaterielList);
        return result;
    }

    public static void updateDumpNumber(Context context, String dumpNumber) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        bean.setStock_transport_id(dumpNumber);
        saveChangeStorageBean(context, bean);
    }

    //获取转储行项目概览
    public static List<OverviewAdapter.OverviewBean> getOverViewBeanWithChangeStorageList(List<ChangeStorageMaterielListBean.MaterielListBean> materielList) {
        List<OverviewAdapter.OverviewBean> resultList = new ArrayList<>();
        for (int i = 0; i < materielList.size(); i++) {
            ChangeStorageMaterielListBean.MaterielListBean materielItem = materielList.get(i);
            if (!TextUtils.isEmpty(materielItem.getNumber())) {
                OverviewAdapter.OverviewBean overItem = new OverviewAdapter.OverviewBean();
                overItem.setInfo(materielItem.getMat_name());
                overItem.setNumber(materielItem.getNumber() + materielItem.getUnit_name());
                resultList.add(overItem);
            }
        }
        return resultList;
    }

    //清空原转储验收人并添加选择用户组的出库验收人
    public static void addGroupChangeStorage (Context context , List<GetOperatorBean.OperatorBean> list) {
        ChangeStorageBean bean = getChangeStorageBean(context);
        bean.setUser_list(list);
        Gson gson = new Gson();
        String json = gson.toJson(bean, ChangeStorageBean.class);
        SPUtils.saveChangeStorageInformation(context, json);
    }


    //通过销售出库用户组获取出库验收人列表
    public static List<GetOperatorBean.OperatorBean> getChangeStorageListByGroupList (Context context , List<GetOperatorGroupBean.OperatorGroupBean> groupListBeanList , String groupCodeStr) {
        List<GetOperatorBean.OperatorBean> list = new ArrayList<>();
        for (int i = 0; i < groupListBeanList.size(); i++) {
            if (groupListBeanList.get(i).getGroup_id().equals(groupCodeStr)) {
                List<GetOperatorBean.OperatorBean> groupList = groupListBeanList.get(i).getGroup_item_list();
                for (int j = 0; j < groupList.size(); j++) {
                    GetOperatorBean.OperatorBean receiverListBean = groupList.get(j);
                    GetOperatorBean.OperatorBean receiveReceiver = new GetOperatorBean.OperatorBean();
                    receiveReceiver.setUser_id(receiverListBean.getUser_id());
                    receiveReceiver.setUser_name(receiverListBean.getUser_name());
                    receiveReceiver.setOrg_name(receiverListBean.getOrg_name());
                    receiveReceiver.setRole_name(receiverListBean.getRole_name());
                    receiveReceiver.setRole_id(receiverListBean.getRole_id());
                    receiveReceiver.setMobile(receiverListBean.getMobile());
                    list.add(receiveReceiver);
                }
            }
        }

        return list;
    }
}
